﻿Type.registerNamespace('SPDesktopContextualTabWebPart');

var _webPartPageComponentId;

SPDesktopContextualTabWebPart.CustomPageComponent = function SPDesktopContextualTabWebPart_CustomPageComponent(webPartPcId) {
    this._webPartPageComponentId = webPartPcId;
    SPDesktopContextualTabWebPart.CustomPageComponent.initializeBase(this);
}

SPDesktopContextualTabWebPart.CustomPageComponent.prototype = {
    init: function SPDesktopContextualTabWebPart_CustomPageComponent$init() { },
    getFocusedCommands: function SPDesktopContextualTabWebPart_CustomPageComponent$getFocusedCommands() {
        return ['SPDesktopContextualTab.EnableCustomTab', 'SPDesktopContextualTab.EnableCustomGroup', 'SPDesktopContextualTab.UpCommand', 'SPDesktopContextualTab.CopyCommand', 'SPDesktopContextualTab.PasteCommand', 'SPDesktopContextualTab.CutCommand', 'SPDesktopContextualTab.DesktopCommand', 'SPDesktopContextualTab.CheckInCommand', 'SPDesktopContextualTab.CheckOutCommand', 'SPDesktopContextualTab.EditCommand', 'SPDesktopContextualTab.DeleteCommand', 'SPDesktopContextualTab.UploadCommand', 'SPDesktopContextualTab.NewFolderCommand'];
    },
    getGlobalCommands: function SPDesktopContextualTabWebPart_CustomPageComponent$getGlobalCommands() {
        return [];
    },
    isFocusable: function SPDesktopContextualTabWebPart_CustomPageComponent$isFocusable() {
        return true;
    },

    canHandleCommand: function SPDesktopContextualTabWebPart_CustomPageComponent$canHandleCommand(commandId) {
        //Contextual Tab commands
        if ((commandId === 'SPDesktopContextualTab.EnableCustomTab') || (commandId === 'SPDesktopContextualTab.EnableCustomGroup') || (commandId === 'SPDesktopContextualTab.UpCommand') || (commandId === 'SPDesktopContextualTab.DesktopCommand')) {
            return true;
        }

        if ((commandId === 'SPDesktopContextualTab.CopyCommand')) {
            if (clickedItem != null && actualQuery != '') {
                return true;
            }
            return false;
        }

        if ((commandId === 'SPDesktopContextualTab.CutCommand')) {
            if (clickedItem != null && actualQuery != '' && $(clickedItem).find('.checkedout').html() != 0) {
                return true;
            }
            return false;
        }

        if (commandId === 'SPDesktopContextualTab.PasteCommand') {
            if (itemToPaste != null && actualQuery != '') {
                return true;
            }
            return false;
        }

        if (commandId === 'SPDesktopContextualTab.CheckInCommand') {
            if (clickedItem != null) {
                if ($(clickedItem).find('.checkedout').html() == 0) {
                    return true;
                }
            }
            return false;
        }

        if (commandId === 'SPDesktopContextualTab.CheckOutCommand') {
            if (clickedItem != null) {
                if ($(clickedItem).find('.checkedout').html() == 2 && $(clickedItem).find('.objecttype').html() == 0) {
                    return true;
                }
            }
            return false;
        }

        if (commandId === 'SPDesktopContextualTab.EditCommand') {
            if (clickedItem != null && $(clickedItem).find('.objecttype').html() != 3) {
                if (forcecheckout == true && $(clickedItem).find('.objecttype').html() != 1) {
                    if ($(clickedItem).find('.checkedout').html() == 0) {
                        return true;
                    }
                    return false;
                }
                return true;
            }
            return false;
        }

        if (commandId === 'SPDesktopContextualTab.DeleteCommand') {
            if (clickedItem != null && $(clickedItem).find('.objecttype').html() != 3) {
                return true;
            }
            return false;
        }

        if (commandId === 'SPDesktopContextualTab.UploadCommand') {
            if (actualQuery != "") {
                return true;
            }
            return false;
        }

        if (commandId === 'SPDesktopContextualTab.NewFolderCommand') {
            if (actualQuery != "") {
                return true;
            }
            return false;
        }
    },

    handleCommand: function SPDesktopContextualTabWebPart_CustomPageComponent$handleCommand(commandId, properties, sequence) {
        if (commandId === 'SPDesktopContextualTab.UpCommand') {
            BrowseUp();
        }
        if (commandId === 'SPDesktopContextualTab.CopyCommand') {
            CopyCut('copy');
        }
        if (commandId === 'SPDesktopContextualTab.CutCommand') {
            CopyCut('cut');
        }
        if (commandId === 'SPDesktopContextualTab.PasteCommand') {
            Paste();
        }
        if (commandId === 'SPDesktopContextualTab.DesktopCommand') {
            Desktop();
        }
        if (commandId === 'SPDesktopContextualTab.CheckInCommand') {
            CheckIn();
        }
        if (commandId === 'SPDesktopContextualTab.CheckOutCommand') {
            CheckOut();
        }
        if (commandId === 'SPDesktopContextualTab.EditCommand') {
            Edit();
        }
        if (commandId === 'SPDesktopContextualTab.DeleteCommand') {
            Delete();
        }
        if (commandId === 'SPDesktopContextualTab.UploadCommand') {
            NewItem();
        }
        if (commandId === 'SPDesktopContextualTab.NewFolderCommand') {
            NewFolder();
        }
    },


    getId: function SPDesktopContextualTabWebPart_CustomPageComponent$getId() {
        return this._webPartPageComponentId;
    }
}

SPDesktopContextualTabWebPart.CustomPageComponent.registerClass('SPDesktopContextualTabWebPart.CustomPageComponent', CUI.Page.PageComponent);
SP.SOD.notifyScriptLoadedAndExecuteWaitingJobs("sharepoint.desktop.contextualtab.js");